<script setup lang="ts">
import { api } from '@/api/api'
import type { Equipment } from '@/types/EquipmentType'
import {
  ElLoading,
  ElMessageBox,
  type FormInstance,
  type FormRules,
  type UploadRequestOptions
} from 'element-plus'
import { reactive, ref } from 'vue'

const form = reactive<Equipment>({
  equipNo: '',
  name: '',
  level: '',
  scope: '',
  addYear: 0,
  traceMethod: '',
  validDate: '',
  source: ''
})

const FormRef = ref<FormInstance>()

const rules = reactive<FormRules<Equipment>>({
  equipNo: [{ required: true, message: '请输入设备仪器编号', trigger: 'blur' }],
  name: [{ required: true, message: '请输入设备仪器名称', trigger: 'blur' }],
  level: [{ required: true, message: '请输入型号/规格/等级', trigger: 'blur' }],
  scope: [{ required: true, message: '请输入测量范围', trigger: 'blur' }],
  addYear: [{ required: true, message: '请输入设备购买年份', trigger: 'blur' }],
  traceMethod: [{ required: true, message: '请输入溯源方式', trigger: 'blur' }],
  validDate: [{ message: '请输入有效截止日期', trigger: 'blur' }],
  source: [{ required: true, message: '请输入设备来源', trigger: 'blur' }]
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl
    .validate((valid) => {
      if (valid) {
        let loading = ElLoading.service()
        api
          .addEquipment(form)
          .then(() => {
            loading.close()
            ElMessageBox.alert('添加成功', '成功', {
              confirmButtonText: '确认'
            })
          })
          .catch(() => {
            loading.close()
            ElMessageBox.alert('添加失败', '错误', {
              confirmButtonText: '确认'
            })
          })
      } else {
        ElMessageBox.alert('设备信息填写出错', '错误', {
          confirmButtonText: '确认'
        })
      }
    })
    .catch(() => {
      ElMessageBox.alert('添加失败', '错误', {
        confirmButtonText: '确认'
      })
    })
}

async function uploadFile(params: UploadRequestOptions) {
  let loading = ElLoading.service()
  let data = new FormData()
  data.append('file', params.file)
  await api
    .addEquipmentByFile(data)
    .then(() => {
      loading.close()
      ElMessageBox.alert('添加成功！', '成功', {
        confirmButtonText: '确认'
      })
    })
    .catch(() => {
      loading.close()
      ElMessageBox.alert('添加失败！', '错误', {
        confirmButtonText: '确认'
      })
    })
}
</script>

<template>
  <div class="main">
    <h1 class="h1">添加设备</h1>
    <el-form :model="form" :rules="rules" label-width="120px" ref="FormRef">
      <el-form-item label="设备仪器编号" prop="equipNo">
        <el-input v-model="form.equipNo" placeholder="请输入设备仪器编号" />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入设备仪器名称" />
      </el-form-item>
      <el-form-item label="型号/规格/等级" prop="level">
        <el-input v-model="form.level" placeholder="请输入型号/规格/等级" />
      </el-form-item>
      <el-form-item label="测量范围" prop="scope">
        <el-input v-model="form.scope" placeholder="请输入测量范围" />
      </el-form-item>
      <el-form-item label="设备购买年份" prop="addYear">
        <el-input v-model="form.addYear" placeholder="请输入设备购买年份" />
      </el-form-item>
      <el-form-item label="溯源方式" prop="traceMethod">
        <el-input v-model="form.traceMethod" placeholder="请输入溯源方式" />
      </el-form-item>
      <el-form-item label="有效截止日期" prop="validDate">
        <el-input v-model="form.validDate" placeholder="请输入有效截止日期" />
      </el-form-item>
      <el-form-item label="设备来源" prop="source">
        <el-input v-model="form.source" placeholder="请输入设备来源" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm(FormRef)" class="_button"
          >提&emsp;&emsp;交</el-button
        >
        <el-upload
          class="_button"
          :limit="1"
          ref="upload"
          :show-file-list="false"
          :http-request="
            (params: UploadRequestOptions) => {
              uploadFile(params)
            }
          "
        >
          <template #trigger>
            <el-button type="primary">选择文件</el-button>
          </template>
        </el-upload>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped>
.h1 {
  margin-left: 50px;
  margin-top: 40px;
  margin-bottom: 50px;
}

._button {
  margin-top: 10px;
  margin-right: 30px;
}

.el-input {
  max-width: max(30vw, 200px);
  min-width: 200px;
}
</style>
